home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Niklas Mellin <niklas.mellin@sto.fdata.se>
- Subject: Re: disgust for goto's
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <316E1773.E87@sto.fdata.se>
- Date: Fri, 12 Apr 1996 08:42:27 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <4kjue7$6ug@bertrand.ccs.carleton.ca>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0 (WinNT; I)
- Organization: WM-data F÷rsvarsdata AB, Sweden
-
- jean richard wrote:
- >
- > Why is the goto command so looked down upon in any language??
- >
- > I remember a class where if you used a goto, you failed the course. :-)
-
- Because it can easily lead to unreadable spaghetti code. And there are
- usually more elegant ways to solve a problem than to use a goto.
-
- void f()
- {
- loopStart:
- // Do something
- goto loopStart;
- }
-
- is equivalent to
-
- void f()
- {
- for (;;)
- {
- // Do something
- }
- }
-
- Sometimes goto can be handy to exit nested loops (exiting
- a normal loop can be done with break), but if the exiting is
- due to an error condition it is IMHO more elegant to use a
- try - catch block around the loops instead. And writing code
- that must exit more than one nesting level in other than
- exceptional cases is usually bad coding I think.
-
- Of cource there are exceptions from every thumb rule. But I
- have never had a need for goto on C++ compilers that can handle
- exceptions.
-
- ---
- Niklas Mellin
-